#include "uicommandmanager.h"
#include "qsignalslotcenter.h"
//#include "hteditordata.h"
//#include "hteditorevents.h"
//#include "htpropertyeditorcommand.h"
#include <assert.h>

namespace QSignalSlotCenter
{
	UICommandManager* UICommandManager::_uiCommandManager = NULL;
	UICommandManager &UICommandManager::GetInstance()
	{
		if(_uiCommandManager==NULL)
			_uiCommandManager = new UICommandManager();
		return *_uiCommandManager;
	}

	UICommandManager::UICommandManager(void):_isCommanding(false),
		_isCreatingMacroCommad(false),_pStackMacrobegin(NULL)
	{
		connect(&QSignalSlotCenter::GetInstance(), SIGNAL(signalCurrentMapChanged()),this, SLOT(clearAllHistories()));
	}

	UICommandManager::~UICommandManager(void)
	{}

	void UICommandManager::runCommand(CommandBase * pCommand)
	{
		if(pCommand != NULL)
		{
			pushCommandtoStack(pCommand);
			enableButtons();
		}
	}

	bool UICommandManager::runCommand(const QString& strCommandName)
	{
		return true;
	}

	void UICommandManager::pushCommandtoStack(CommandBase* pCommand)
	{
		assert(pCommand != NULL);
		if(!_isCreatingMacroCommad)
		{
			if(!pCommand->canRedo()||!_undoGroup.activeStack())
			{
				QUndoStack* pStack = new QUndoStack(&_undoGroup);
				_undoGroup.addStack(pStack);
				_undoGroup.setActiveStack(pStack);
			}
		}
		_undoGroup.activeStack()->push(pCommand);
		enableButtons();
	}

	void UICommandManager::beginMacroCommand(const QString &strMacroCommandName)
	{
		if(!_undoGroup.activeStack())
		{
			QUndoStack* pStack = new QUndoStack(&_undoGroup);
			_undoGroup.addStack(pStack);
			_undoGroup.setActiveStack(pStack);
		}
		_isCreatingMacroCommad = true;
		_pStackMacrobegin = _undoGroup.activeStack();
		_pStackMacrobegin->beginMacro(strMacroCommandName);
	}

	void UICommandManager::endMacroCommand()
	{	
		assert(_pStackMacrobegin == _undoGroup.activeStack());
		_pStackMacrobegin->endMacro();
		_isCreatingMacroCommad = false;
		_pStackMacrobegin = NULL;
		enableButtons();
	}

	void UICommandManager::doRedo()
	{
		if(!_isCommanding)
		{
			_undoGroup.redo();
			enableButtons();
		}
	}

	void UICommandManager::doUndo()
	{
		if(!_isCommanding)
		{
			_undoGroup.undo();
			enableButtons();
		}
	}

	void UICommandManager::clearAllHistories()
	{
		QList<QUndoStack*> listUndo = _undoGroup.stacks();
		QUndoStack* pStack = NULL;
		foreach(pStack,listUndo)
		{
			_undoGroup.removeStack(pStack);
		}
		enableButtons();
	}

	void UICommandManager::enableButtons()
	{
		int undostate = _undoGroup.canUndo()?2:0;
		int redostate = _undoGroup.canRedo()?2:0;
		QSignalSlotCenter::GetInstance().emitChangeUndoRedoActionState(ACTION_UNDO_EVENT,undostate);
		QSignalSlotCenter::GetInstance().emitChangeUndoRedoActionState(ACTION_REDO_EVENT,redostate);
	}

	void UICommandManager::lockIsCommanding()
	{
		_isCommanding = true;
	}

	void UICommandManager::unLockIsCommanding()
	{
		_isCommanding = false;
	}
}